//
//  DYVolumeView.m
//  DYPlayer
//
//  Created by 黄德玉 on 2017/7/5.
//  Copyright © 2017年 none. All rights reserved.
//

#import "DYVolumeView.h"
#import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>
#import <objc/runtime.h>

@interface DYVolumeView ()

@property (nonatomic,strong) UISlider * slider;

@property (nonatomic,strong)MPVolumeView * volumeView;

@end

@implementation DYVolumeView


const char * kVolume;

+ (void)updateVolumeWithValue:(CGFloat)value{
    UIWindow * keyWindow = [UIApplication sharedApplication].keyWindow;
    DYVolumeView * v = objc_getAssociatedObject(keyWindow, &kVolume);
    if (!v) {
        v = [[DYVolumeView alloc] init];
        objc_setAssociatedObject(keyWindow, &kVolume, v, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    v.slider.value += value;
}



- (UISlider *)slider{
    if (!_slider) {
        self.volumeView = [[MPVolumeView alloc] init];

        for (UIView *view in self.volumeView.subviews){
            if ([view.class.description isEqualToString:@"MPVolumeSlider"]){
                _slider = (UISlider *)view;
                break;
            }
        }
        // 使用这个category的应用不会随着手机静音键打开而静音，可在手机静音下播放声音
        NSError *setCategoryError = nil;
        BOOL success = [[AVAudioSession sharedInstance]
                        setCategory: AVAudioSessionCategoryPlayback
                        error: &setCategoryError];
        
        if (!success) {
        }

    }
    return _slider;
}




@end
